# -*- coding: utf-8 -*- 
# @project : 《Atcoder》
# @Author : created by bensonrachel on 2021/9/24
# @File : 11.B - Frog 2.py
import sys
MaxInt = sys.maxsize


def frog_2():
    dp = [0] * (n + 1)
    dp[1] = 0
    dp[2] = abs(a[2] - a[1])
    for i in range(3, n + 1):
        temp = min(k,i-1)
        total = MaxInt
        for j in range(1,temp+1):
            x = dp[i - j] + abs(a[i] - a[i - j])
            if(x<total):
                total = x
        dp[i] = total
    return dp[n]



"""
青蛙跳台阶，一维线性dp，记录跳到每一个位置的最小的total_cost。每次能跳1到k格，因为k不大，所以加一层循环。
"""
if __name__ == '__main__':
    n,k = map(int,input().split())
    a = [int(i) for i in input().split()]
    a = [0] + a
    ans = frog_2()
    print(ans)